﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Entity;
using HRM.BIZ;
using HRM.Utility;

namespace WebUI.Pages
{
    public partial class EmployeeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
                LoadEmployeesList();
            }
        }

        private void LoadEmployeesList()
        {
            EmployeeBiz empBiz = new EmployeeBiz();
            //var empList = empBiz.GetAll(ddlStatus.SelectedValue);
            var empList = empBiz.GetEmployees().Tables[0];
            var dvData = new DataView(empList);
            dvData.RowFilter = BuildFilterString() ;// +ddlStatus.SelectedValue;
            ucEmployeeList.EmployeeDataSource = dvData;
            ucEmployeeList.BindData();
        }

        protected void OnFilterChange(object sender, EventArgs e)
        {
            LoadEmployeesList();
        }

        protected void InitData()
        {
            var positions = new PositionBiz().GetAll();
            positions.Insert(0, new Position() { ID = -1, Name = "Tất cả" });
            ddlPosition.DataSource = positions;
            ddlPosition.DataBind();

            var departments = new DepartmentBiz().GetAll();
            departments.Insert(0, new Department() { ID = -1, Name = "Tất cả" });
            ddlDepartment.DataSource = departments;
            ddlDepartment.DataBind();
        }

        protected string BuildFilterString()
        {
            string filter = "Status {0} {1} AND Position {2} {3} AND Department {4} {5}";
            if (ddlStatus.SelectedValue.ToInt32() == -1)
            {
                filter = filter.Replace("{0}", ">");
                filter = filter.Replace("{1}", "-1");
            }
            else
            {
                filter = filter.Replace("{0}", "=");
                filter = filter.Replace("{1}", ddlStatus.SelectedValue);
            }

            if (ddlPosition.SelectedValue.ToInt32() == -1)
            {
                filter = filter.Replace("{2}", ">");
                filter = filter.Replace("{3}", "-1");
            }
            else
            {
                filter = filter.Replace("{2}", "=");
                filter = filter.Replace("{3}", ddlPosition.SelectedValue);
            }

            if (ddlDepartment.SelectedValue.ToInt32() == -1)
            {
                filter = filter.Replace("{4}", ">");
                filter = filter.Replace("{5}", "-1");
            }
            else
            {
                filter = filter.Replace("{4}", "=");
                filter = filter.Replace("{5}", ddlDepartment.SelectedValue);
            }

            return filter;
        }
    }
}
